package com.zjsd.sqeybusiness.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.mapper.PayrollMapper;
import com.zjsd.sqeybusiness.domain.Payroll;
import com.zjsd.sqeybusiness.service.IPayrollService;

/**
 * 工资表Service业务层处理
 *
 * @author Zheng
 * @date 2021-09-28
 */
@Service
public class PayrollServiceImpl implements IPayrollService
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private PayrollMapper payrollMapper;


    /**
     * 查询工资表
     *
     * @param payrollid 工资表ID
     * @return 工资表
     */
    @Override
    public Payroll selectPayrollById(Long payrollid)
    {
        return payrollMapper.selectPayrollById(payrollid);
    }

    /**
     * 查询工资表列表
     *
     * @param payroll 工资表
     * @return 工资表
     */
    @Override
    public List<Payroll> selectPayrollList(Payroll payroll)
    {
        return payrollMapper.selectPayrollList(payroll);
    }

    /**
     * 新增工资表
     *
     * @param payroll 工资表
     * @return 结果
     */
    @Override
    public int insertPayroll(Payroll payroll)
    {
        // 验证是否存在这个用户
        Payroll payroll1 = payrollMapper.selectPayrollByInfo(payroll);
        int i = 0 ;

        if (StringUtils.isNull(payroll1))
        {
            SysUser sysUser = payrollMapper.querySysUser(payroll);
            if(sysUser != null){
                payroll.setUserid(sysUser.getUserId());
            }
            payroll.setXfflag("0");
            payroll.setQrflag("0");
            payroll.setDrtime(new Date());
            if(StringUtils.isNotBlank(payroll.getName())){
                i = payrollMapper.insertPayroll(payroll);
            }
        }

        return i;
    }

    /**
     * 修改工资表
     *
     * @param payroll 工资表
     * @return 结果
     */
    @Override
    public int updatePayroll(Payroll payroll)
    {
        SysUser sysUser = payrollMapper.querySysUser(payroll);
        if(sysUser != null){
            payroll.setUserid(sysUser.getUserId());
        }
        return payrollMapper.updatePayroll(payroll);
    }

    /**
     * 批量删除工资表
     *
     * @param payrollids 需要删除的工资表ID
     * @return 结果
     */
    @Override
    public int deletePayrollByIds(Long[] payrollids)
    {
        return payrollMapper.deletePayrollByIds(payrollids);
    }

    /**
     * 删除工资表信息
     *
     * @param payrollid 工资表ID
     * @return 结果
     */
    @Override
    public int deletePayrollById(Long payrollid)
    {
        return payrollMapper.deletePayrollById(payrollid);
    }

    @Override
    public String importPayroll(List<Payroll> payrollList, boolean updateSupport, String operName) {

        if (StringUtils.isNull(payrollList) || payrollList.size() == 0)
        {
            throw new CustomException("工资表数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Payroll payroll : payrollList)
        {
            try
            {
                // 验证是否存在这个用户
                Payroll payroll1 = payrollMapper.selectPayrollByInfo(payroll);

                if (StringUtils.isNull(payroll1))
                {
                    SysUser sysUser = payrollMapper.querySysUser(payroll);
                    if(sysUser != null){
                        payroll.setUserid(sysUser.getUserId());
                    }
                    payroll.setXfflag("0");
                    payroll.setQrflag("0");
                    payroll.setDrtime(new Date());
                    if(StringUtils.isNotBlank(payroll.getName())){
                        this.insertPayroll(payroll);
                    }
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + payroll.getName() + " 工资表导入成功");
                }
                else if (updateSupport)
                {
                    SysUser sysUser = payrollMapper.querySysUser(payroll);
                    if(sysUser != null){
                        payroll.setUserid(sysUser.getUserId());
                    }
                    payroll.setXfflag("0");
                    payroll.setQrflag("0");
                    payroll.setDrtime(new Date());
                    this.updatePayroll(payroll);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + payroll.getName() + " 工资更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、员工 " + payroll.getName() + " 工资已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工 " + payroll.getName() + " 工资导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int sendPayrollByIds(Long[] payrollids) {
        return payrollMapper.sendPayrollByIds(payrollids);
    }

    @Override
    public List<Payroll> queryUserPayroll(Payroll payroll) {


        List<Payroll> list = payrollMapper.queryUserPayroll(payroll);

        return list;
    }

    @Override
    public int confirmPayroll(Payroll payroll) {
        payroll.setQrtime(new Date());
       int i=  payrollMapper.confirmPayroll(payroll);
        return i;
    }
}
